package org.jeecg.modules.kuner.hirebase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.Strings;
import org.jeecg.modules.kuner.browsebase.service.IKrBrowseBaseService;
import org.jeecg.modules.kuner.collectionbase.service.IKrCollectionBaseService;
import org.jeecg.modules.kuner.hirebase.entity.KrHireBase;
import org.jeecg.modules.kuner.hirebase.mapper.KrHireBaseMapper;
import org.jeecg.modules.kuner.hirebase.service.IKrHireBaseService;
import org.jeecg.modules.kuner.hirebase.vo.KrHireDetailVO;
import org.jeecg.modules.kuner.hirebase.vo.KrHireVO;
import org.jeecg.modules.putil.KrStatic;
import org.jeecg.modules.putil.LoginUserUtil;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @Description: 招聘信息
 * @Author: jeecg-boot
 * @Date: 2025-02-18
 * @Version: V1.0
 */
@Service
public class KrHireBaseServiceImpl extends ServiceImpl<KrHireBaseMapper, KrHireBase> implements IKrHireBaseService {

    @Resource
    private IKrCollectionBaseService collectionBaseService;
    @Resource
    private LoginUserUtil loginUserUtil;
    @Resource
    private IKrBrowseBaseService browseBaseService;

    /**
     * 混合查询
     *
     * @param mixSearch  文本搜索
     * @param money      金额少选
     * @param searchType 混合查询
     * @param pageNo     分页页码
     * @param pageSize   分页大小
     * @return 查询结果
     */
    @Override
    public IPage<KrHireVO> mixSearch(
            String mixSearch,
            BigDecimal money,
            KrStatic.SearchType searchType,
            String type,
            Integer edu,
            String workCity,
            Integer pageNo,
            Integer pageSize
    ) {
        IPage<KrHireVO> page = new Page<>(pageNo, pageSize);
        switch (searchType) {
            case MONEY:
                page = this.baseMapper.searchSortByMoney(mixSearch, type, edu, money, workCity, page);
            case CREATE_TIME:
                page = this.baseMapper.searchSortByCreateTime(mixSearch, type, edu, money, workCity, page);
        }
        return page;
    }

    /**
     * 查询详情
     *
     * @param id 被查询ID
     * @return 查询结果
     */
    @Override
    public KrHireDetailVO detail(String id) {
        browseBaseService.visitContent(id, CommonConstant.VISIT_TYPE_ZW, CommonConstant.TIME_MODIFY_DECREASE_ZW);
        return this.baseMapper.detail(id);

    }
    /**
     * 去收藏
     *
     * @param hireId 被收藏的职位ID
     */
    @Override
    public void doCollection(String hireId) {
        getOptById(hireId).orElseThrow(() -> new JeecgBootException("找不到被收藏的职位信息！"));
        collectionBaseService.collection(loginUserUtil.getLoginUser().getId(), hireId, KrStatic.ContentType.HIRE);
    }

    /**
     * 取消收藏
     *
     * @param hireId 被收藏的职位ID
     */
    @Override
    public void cancelCollection(String hireId) {
        collectionBaseService.unCollection(loginUserUtil.getLoginUser().getId(), hireId, KrStatic.ContentType.HIRE);
    }

    /**
     * 修改或者发布一个岗位
     *
     * @param hireBase 被发布的岗位信息
     */
    @Override
    public void modifyHire(KrHireBase hireBase) {
        if (Strings.isNotBlank(hireBase.getId())) {
            updateById(hireBase);
            return;
        }
        hireBase.setPublishUser(loginUserUtil.getLoginUser().getId());
        hireBase.setStatus(CommonConstant.STATUS_DRAFT);
        save(hireBase);
    }
}
